package com.team.web;


import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.team.dao.GoalDao;
import com.team.dao.TaskDao;
import com.team.dao.UserDao;
import com.team.entity.Goal;
import com.team.entity.Task;
import com.team.entity.User;
import com.team.util.Const;
import com.team.util.StringUtil;

public class TaskServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String method = request.getParameter("_m");
		if("save".equals(method)){
			saveTask(request,response);
		}else if("select".equals(method)){
			selectAllTasks(request,response);
			
		}else if("edit".equals(method)){
			editTask(request,response);
			
		}else if("update".equals(method)){
			update(request,response);
		}else {
			response.sendError(404,"request a error path");
			return ;
		}
	
	}
	
	private void update(HttpServletRequest request, HttpServletResponse response) throws IOException {
		
		String taskId = request.getParameter("id");
		int id = Integer.parseInt(taskId);
		TaskDao td = new TaskDao();
		Task tempTask = td.selectTaskById(id);
		GoalDao gd = new GoalDao();
		Goal  goal = gd.selectGoalById(tempTask.getGoalId());
		System.out.println("goalId update "+goal.getId());
		System.out.println("taskId"+taskId);
		String name=request.getParameter("name");
		String createtime=request.getParameter("createtime");
		String begtime=request.getParameter("begtime");
		String endtime=request.getParameter("endtime");
		String state=request.getParameter("state");
		String level=request.getParameter("level");
		String rate=request.getParameter("rate");
		String leader=request.getParameter("leader");
		
	/*	TaskDao td;
		td = new TaskDao();
		Goal goal;
		try {
			goal = (Goal) session.getAttribute(Const.SESSION_GOALTASK);
			System.out.println("goalid update"+ goal.getId());
		} catch (Exception e) {
			e.printStackTrace();
		}*/
		
		String content=request.getParameter("content");
		if(StringUtil.isEmpty(state)||StringUtil.isEmpty(content)
				||StringUtil.isEmpty(leader)||StringUtil.isEmpty(rate)
				||StringUtil.isEmpty(rate)||StringUtil.isEmpty(level)
				||StringUtil.isEmpty(endtime)||StringUtil.isEmpty(begtime)
				||StringUtil.isEmpty(createtime)||StringUtil.isEmpty(name)
				||StringUtil.isEmpty(taskId)){
			response.sendRedirect("editTask.jsp");
			return ;
			
		}
		System.out.println("name");
		System.out.println("leader");
		
		
		UserDao ud = new UserDao();
		User temp = ud.selectByName(leader);
		Task task = new Task();
		
		
		task.setBegTime(begtime);
		task.setContent(content);
		task.setCreateTime(createtime);
		task.setEndTime(endtime);
		task.setLevel(level);
		task.setRate(rate);
		task.setState(state);
		task.setTaskName(name);
		task.setUserId(temp.getId());
		task.setId(id);
		task.setGoalId(goal.getId());
		
		int num = td.update(task);
		if(num > 0){
			System.out.println("goalID"+goal.getId());
			response.sendRedirect("task.jspx?_m=select&goalid="+goal.getId());
			return ;
		}else {
			System.out.println("update task error");
		}
		
	}

	private void editTask(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String taskId = request.getParameter("id");
		int id = Integer.parseInt(taskId);
		
		TaskDao td = new TaskDao();
		
		Task task = td.selectTaskById(id);
		
		
		if(task != null){
			System.out.println("task edit find succ");
			UserDao ud = new UserDao();
			User temp  = ud.selectById(task.getUserId());
			HttpSession session = request.getSession();
			session.setAttribute(Const.SESSION_GOALEDITUSER, temp);
			session.setAttribute(Const.SESSION_EDITASK, task);
			response.sendRedirect("editTask.jsp");
			return;
		}else {
			response.sendRedirect("task.jsp");
			return ;
		}
		
		
	}
	private void selectAllTasks(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		
		String goalid = request.getParameter("goalid");
		int id = Integer.parseInt(goalid);
		TaskDao td = new TaskDao();
		UserDao ud = new UserDao();
		List<Task> list = td.selectAllByGoalId(id);
		if(list  != null){
			Map<Task,User> map = new HashMap<Task,User>();
			for(Task task : list){
				User temp = ud.selectById(task.getUserId());
				if(temp != null){
					map.put(task, temp);
				}else {
					System.out.println(task.getUserId()+"涓嶅瓨鍦�");
				}
			}
			
			HttpSession session = request.getSession();
			session.setAttribute(Const.SESSION_TASKSMAP, map);
			session.setAttribute(Const.SESSION_TASKS, list);
			response.sendRedirect("task.jsp");
			return ;
		}else {
			System.out.println("check task list error");
		}
		
	}
	private void saveTask(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		
		String name = request.getParameter("name");
		String content = request.getParameter("content");
		String state = request.getParameter("state");
		String begtime = request.getParameter("begtime");
		String endtime = request.getParameter("endtime");
		String createtime = request.getParameter("createtime");
		String level = request.getParameter("level");
		String rate = request.getParameter("rate");
		
		if(StringUtil.isEmpty(name)||StringUtil.isEmpty(content)
				||StringUtil.isEmpty(state)||StringUtil.isEmpty(createtime)
				||StringUtil.isEmpty(endtime)||StringUtil.isEmpty(begtime)
				||StringUtil.isEmpty(rate)||StringUtil.isEmpty(level)){
			response.sendRedirect("newtask.jsp?code=1001");
			return ;
		}
		HttpSession session = request.getSession();
		Goal goal = (Goal) session.getAttribute(Const.SESSION_SAVEGOAL);
		
		User user = (User)session.getAttribute(Const.SESSION_USER);
		
		Task task = new Task();
		task.setTaskName(name);
		task.setContent(content);
		task.setState(state);
		task.setBegTime(begtime);
		task.setCreateTime(createtime);
		task.setEndTime(endtime);
		task.setLevel(level);
		task.setRate(rate);
		task.setUserId(user.getId());
		task.setGoalId(goal.getId());
		
		TaskDao td = new TaskDao();
		int num = td.saveTask(task);
		if(num > 0){
			response.sendRedirect("task.jspx?_m=select&goalid="+goal.getId());
			return ;
		}else {
			System.out.println("task save fail");
		}
		
	}
}
